<template>
  <div class='sub-sort'>
    <div class="sort">
      <a
        href="javascript:;"
        :class="{active:reqParams.sortField===''}"
        @click="changeSort('')"
      >默认排序</a>
      <a
        href="javascript:;"
        :class="{active:reqParams.sortField==='publishTime'}"
        @click="changeSort('publishTime')"
      >最新商品</a>
      <a
        href="javascript:;"
        :class="{active:reqParams.sortField==='orderNum'}"
        @click="changeSort('orderNum')"
      >最高人气</a>
      <a
        href="javascript:;"
        :class="{active:reqParams.sortField==='evaluateNum'}"
        @click="changeSort('evaluateNum')"
      >评论最多</a>
      <a
        href="javascript:;"
        @click="changeSort('price')"
      >
        价格排序
        <i
          class="arrow up"
          :class="{active:reqParams.sortMethod==='asc'}"
        />
        <i
          class="arrow down"
          :class="{active:reqParams.sortMethod==='desc'}"
        />
      </a>
    </div>
    <div class="check">
      <XtxCheckbox v-model="reqParams.inventory">仅显示有货商品</XtxCheckbox>
      <XtxCheckbox v-model="reqParams.onlyDiscount">仅显示特惠商品</XtxCheckbox>
    </div>
  </div>
</template>
<script>
import { reactive, watch } from 'vue'
export default {
  name: 'SubSort',
  setup (props, { emit }) {
    const reqParams = reactive({
      // 是否有库存
      inventory: false,
      // 是否只显示优惠
      onlyDiscount: false,
      // 排序字段 publishTime,orderNum,price,evaluateNum]
      sortField: '',
      // 正序/倒叙 asc为正序，desc为倒序，默认为desc
      sortMethod: ''
    })
    /** 处理排序 */
    const changeSort = (sortField) => {
      reqParams.sortField = sortField
      if (reqParams.sortField === 'price') {
        if (reqParams.sortMethod === '') {
          reqParams.sortMethod = 'desc'
        } else {
          reqParams.sortMethod = reqParams.sortMethod === 'desc' ? 'asc' : 'desc'
        }
      } else {
        reqParams.sortMethod = ''
      }
    }
    watch(reqParams, () => {
      emit('change', reqParams)
    }, { deep: true })
    return { reqParams, changeSort }
  }
}
</script>
<style scoped lang='less'>
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all 0.3s;
      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
          &.active {
            border-bottom-color: @xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>
